package com.zhang.zhangapp.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhang.zhangapp.MainActivity;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.config.HttpConfig;
import com.zhang.zhangapp.bean.user.User;
import com.zhang.zhangapp.view.mine.MineBankListActivity;
import com.zhang.zhangapp.view.mine.MineBillsActivity;
import com.zhang.zhangapp.view.mine.MineCashCouponActivity;
import com.zhang.zhangapp.view.mine.MineCollectActivity;
import com.zhang.zhangapp.view.mine.MineInfoActiviy;
import com.zhang.zhangapp.view.mine.RechargeActivity;
import com.zhang.zhangapp.view.mine.SettingActivity;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by 罗肖 on 2018/3/28.
 */
@ContentView(R.layout.fragment_mine)
public class MineFragment extends BaseLazyFragment {

    @ViewInject(R.id.action_bar_right_image)
    ImageView settingImage;
    @ViewInject(R.id.mine_image)
    ImageView mineImage;
    @ViewInject(R.id.mine_name)
    TextView mineNameText;
    @ViewInject(R.id.mine_tel)
    TextView mineTelText;
    /*
    余额
     */
    @ViewInject(R.id.mine_amount)
    TextView mineAmountText;
    /*
    积分
     */
    @ViewInject(R.id.mine_point)
    TextView minePointText;
    /*
     优惠券
      */
    @ViewInject(R.id.mine_coupon)
    TextView mineCouponText;

    private User user;

    ImageOptions imageOptions;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initImageOption();
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onFirstUserVisible() {

    }

    private void initImageOption() {
        imageOptions = new ImageOptions.Builder()
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setRadius(DensityUtil.dip2px(100))
                .setCrop(true)//是否对图片进行裁剪
                .setFailureDrawableId(R.mipmap.header_default)
                .setLoadingDrawableId(R.mipmap.header_default)
                .build();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MApplication.getInstance().getUser() != null) {
            user = MApplication.getInstance().getUser();
            x.image().bind(mineImage, HttpConfig.IMAGE_HTTP_PATH + user.getPhoto(), imageOptions);
            mineTelText.setText(user.getMobilenum());
            mineNameText.setText((user.getNickname() != null && user.getNickname().length() > 0) ? user.getNickname() : user.getMobilenum());
            minePointText.setText(user.getIntegral());
            mineAmountText.setText(user.getBalance());
            mineCouponText.setText(user.getTickets());
        } else {
            ((MainActivity) getActivity()).selectHomeIndex();
        }
    }

    @Event(type = View.OnClickListener.class, value = {R.id.mine_image, R.id.mine_layout, R.id.mine_pay, R.id.mine_bank_car,
            R.id.mine_collection, R.id.mine_bill_detail, R.id.mine_point_shop, R.id.action_bar_right_image,R.id.coupon_layout})
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.mine_image:
                break;
            case R.id.mine_pay:
                openRechargeActivity();
                break;
            case R.id.mine_bank_car:
                openMineBankList();
                break;
            case R.id.mine_collection:
                openCollectActivity();
                break;
            case R.id.mine_bill_detail:
                openMineBillsActivity();
                break;
            case R.id.mine_point_shop:
                break;
            case R.id.action_bar_right_image:
                // 設置
                openSettingActivity();
                break;
            case R.id.mine_layout:
                openMyselfActivity();
                break;
            case R.id.coupon_layout:
                openCouponListActivity();
                break;
        }
    }

    private void openMineBankList(){
        Intent intent = new Intent(this.getActivity(),MineBankListActivity.class);
        startActivity(intent);
    }

    /**
     * 我的消费
     */
    private void openMineBillsActivity(){
        Intent intent = new Intent(this.getActivity(),MineBillsActivity.class);
        startActivity(intent);
    }
    private void openCouponListActivity(){
        Intent intent = new Intent(getActivity(), MineCashCouponActivity.class);
        startActivity(intent);
    }

    private void openRechargeActivity() {
        Intent intent = new Intent(getActivity(), RechargeActivity.class);
        startActivity(intent);
    }

    private void openCashCouponActivity() {
        Intent intent = new Intent(getActivity(), MineCashCouponActivity.class);
        startActivity(intent);
    }

    /**
     * 跳轉收藏
     */
    private void openCollectActivity() {
        Intent intent = new Intent(getActivity(), MineCollectActivity.class);
        startActivity(intent);
    }

    private void openMyselfActivity() {
        Intent intent = new Intent(getActivity(), MineInfoActiviy.class);
        startActivity(intent);
    }

    /**
     * 跳转设置页面
     */
    private void openSettingActivity() {
        Intent intnt = new Intent(this.getActivity(), SettingActivity.class);
        getActivity().startActivity(intnt);
    }


    //1.在Fragment中使用startActivityForResult的时候，不要使用getActivity().startActivityForResult,而是应该直接使startActivityForResult()。
    //2.如果activity中重写了onActivityResult，那么activity中的onActivityResult一定要加上super.onActivityResult(requestCode, resultCode, data)。
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
        }
    }
}
